﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.FindArtInfo;
using BusinessLayer.FindArtInfo.Models;
using CommandLib;
using FindArtInfo.Common;
using FindArtInfo.Models;

namespace FindArtInfo.Controllers
{
    public class PricesController : MultiBase
    {
        public ActionResult Result(string priceTitle, int? pageIndex, int? order)
        {
            int pageSize = 30;
            PagePriceSearchResultModel model = new PagePriceSearchResultModel();            
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;
            model.PriceSearchResultKeyWord = priceTitle;
            model.ListPrice = new PricesBL().Search(priceTitle, order, Convert.ToInt32(pageIndex == null ? 1 : pageIndex),
                                                      pageSize);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_SEARCH] == null)
            {
                int totalRecord;
                new PricesBL().GetCountByKeyWord(priceTitle, out totalRecord);
                Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_SEARCH] = totalRecord;
            }
            model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_SEARCH]);
            model.TotalPage = model.TotalRecord%model.PageSize == 0
                                  ? model.TotalRecord/model.PageSize
                                  : (model.TotalRecord/model.PageSize) + 1;
            model.Url = string.Format("/{0}/{1}?priceTitle={2}&order={3}", "Prices", "Result",
                                      HttpUtility.UrlEncode(priceTitle), order);
            return View(model);
        }

        public ActionResult ListPrices(int artistsId, int? pageIndex, int? order)
        {
            int pageSize = 30;
            PagePriceModel model = new PagePriceModel();
            model.Artists = new ArtistsBL().GetById(artistsId);            
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;

            model.ListPrice = new PricesBL().GetByArtistsId(artistsId, order, Convert.ToInt32(pageIndex == null ? 1 : pageIndex),
                                                      pageSize);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_SEARCH] == null)
            {
                int totalRecord;
                new PricesBL().GetCountByArtistsId(artistsId, out totalRecord);
                Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_BY_ARTISTID] = totalRecord;
            }
            model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_BY_ARTISTID]);
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            model.Url = order == null ? string.Format("/list-prices-by-artist/1/{0}/{1}", artistsId, Utils.UnicodeToKoDauAndGach(model.Artists.FullName)) :
                                        string.Format("/list-prices-by-artist/{0}/{1}/{2}", order, artistsId, Utils.UnicodeToKoDauAndGach(model.Artists.FullName));

            return View(model);
        }

        public ActionResult ListPricesHasArtWork(int artistsId, int? pageIndex, int? order)
        {
            int pageSize = 30;
            PagePriceModel model = new PagePriceModel();
            model.Artists = new ArtistsBL().GetById(artistsId);
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;

            model.ListPrice = new PricesBL().GetAllPriceHasArtWorkByArtistsId(artistsId, order, Convert.ToInt32(pageIndex == null ? 1 : pageIndex),
                                                      pageSize);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_SEARCH] == null)
            {
                int totalRecord;
                new PricesBL().GetCountPriceHasArtWokByArtistsId(artistsId, out totalRecord);
                Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_BY_ARTISTID] = totalRecord;
            }
            model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PRICE_BY_ARTISTID]);
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            //model.Url = string.Format("/{0}/{1}?artistsId={2}&order={3}", "Prices", "ListPricesHasArtWork",
            //                          artistsId, order);
            model.Url = order == null ? string.Format("/list-prices-has-art-work-by-artist/1/{0}/{1}", artistsId, Utils.UnicodeToKoDauAndGach(model.Artists.FullName)) :
                                       string.Format("/list-prices-has-art-work-by-artist/{0}/{1}/{2}", order, artistsId, Utils.UnicodeToKoDauAndGach(model.Artists.FullName));

            return View(model);
        }
        
        [CheckExpriesDateAttribute]
        public ActionResult Detail(int priceId)
        {
            PagePriceDetailModel model = new PagePriceDetailModel();
            var price = new PricesBL().GetById(priceId);
            model.Artist = new ArtistsBL().GetById(price.ArtistId);
            model.Price = price;
            model.ListArtWork = new ArtWorkBL().GetByPriceId(price.ID);
            return View(model);
        }

    }
}
